package com.lst.demo.utils;

import android.app.KeyguardManager;
import android.content.Context;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import androidx.core.hardware.fingerprint.FingerprintManagerCompat;
import androidx.core.os.CancellationSignal;

import com.lst.demo.R;


public class FingerPrintUtils {
 
    private FingerprintManagerCompat mFingerprintManagerCompat;
    private CancellationSignal mCancellationSignal;
    private Context context;
 
    public FingerPrintUtils(Context context) {
        this.context = context;
        if (mFingerprintManagerCompat == null || mCancellationSignal == null) {
            mFingerprintManagerCompat = FingerprintManagerCompat.from(context);
            mCancellationSignal = new CancellationSignal();
        }
    }
 
    public interface FingerPrintResult {
        void success();
 
        void error(int code, CharSequence info);
 
        void retry(int code, CharSequence info);
    }
 
    /**
     * 关闭
     *
     * @return
     */
    public boolean cancelCallback() {
        if (mCancellationSignal != null && !mCancellationSignal.isCanceled()) {
            mCancellationSignal.cancel();
            mCancellationSignal = null;
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 开始
     */
    public void start(final FingerPrintResult fpResult) {
        if (fpResult == null) {
            throw new RuntimeException("FingerPrintUtils.init()参数Context或者FingerPrintResult为null");
        }
        if (mFingerprintManagerCompat.isHardwareDetected() && mFingerprintManagerCompat.hasEnrolledFingerprints()) {
            mFingerprintManagerCompat.authenticate(null, 0, mCancellationSignal, new FingerprintManagerCompat.AuthenticationCallback() {
                @Override
                public void onAuthenticationError(int errorCode, CharSequence errString) {
                    Log.d("-=-", "onAuthenticationError" + errString);
                    super.onAuthenticationError(errorCode, errString);
                    if (errorCode == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT) {
                        fpResult.error(FingerprintManager.FINGERPRINT_ERROR_LOCKOUT, errString);
                    }// else {
//                        fpResult.retry(errorCode, errString);
//                    }
                }
 
                @Override
                public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
                    super.onAuthenticationHelp(helpCode, helpString);
                    Log.d("-=-", "onAuthenticationHelp" + helpString);
//                    fpResult.retry(helpCode, helpString);
                }
 
                @Override
                public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
                    super.onAuthenticationSucceeded(result);
                    Log.d("-=-", "onAuthenticationSucceeded" + result);
                    fpResult.success();
                }
 
                @Override
                public void onAuthenticationFailed() {
                    super.onAuthenticationFailed();
                    Log.d("-=-", "onAuthenticationFailed");
                    fpResult.retry(0, "authentication failed");
                }
            }, null);
        } else {
            fpResult.error(-1, context.getString(R.string.fingerprint_recognition_not_support));
        }
    }
 
    /**
     * 判断是否支持指纹识别
     */
    public boolean supportFingerprint() {
        if (Build.VERSION.SDK_INT < 23) {
            Toast.makeText(context, context.getString(R.string.fingerprint_recognition_not_support), Toast.LENGTH_SHORT).show();
            return false;
        } else {
            KeyguardManager keyguardManager = context.getSystemService(KeyguardManager.class);
            FingerprintManagerCompat fingerprintManager = FingerprintManagerCompat.from(context);
            if (!fingerprintManager.isHardwareDetected()) {
                Toast.makeText(context, context.getString(R.string.fingerprint_recognition_not_support), Toast.LENGTH_SHORT).show();
                return false;
            } else if (keyguardManager != null && !keyguardManager.isKeyguardSecure()) {
                Toast.makeText(context, context.getString(R.string.fingerprint_recognition_not_support), Toast.LENGTH_SHORT).show();
                return false;
            } else if (!fingerprintManager.hasEnrolledFingerprints()) {
                Toast.makeText(context, context.getString(R.string.fingerprint_recognition_not_enrolled), Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return true;
    }
}